'use strict';
// eslint-disable-next-line jsdoc/require-param

const createValidate = (ctx) => {
  ctx.validate({
    name: {
      type: 'string',
      min: 1,
      max: 20,
    },
    index: {
      type: 'number',
      min: 0,
      max: 9,
      convertType: 'number',
    },
    describe: {
      type: 'string',
      max: 140,
      required: false,
    },
    group: {
      type: 'boolean',
    },
    root: {
      type: 'boolean',
    },
  });
  if (!ctx.w_body().group) {
    ctx.validate({
      type: {
        type: 'enum',
        values: [ 'GET', 'PUT', 'POST', 'DELETE' ],
      },
      path: {
        type: 'string',
        min: 1,
        max: 100,
      },
      superId: {
        type: 'mongo-id',
        required: false,
      },
    });
  }
}

/**
 * 增加数据
 */
exports.create = async ctx => {
  createValidate(ctx)
  const data = await ctx.service.authApi.create();
  ctx.sendSuccess(data);
};

exports.index = async ctx => {
  const data = await ctx.service.authApi.all();
  ctx.sendSuccess(data);
};

// eslint-disable-next-line jsdoc/require-param
/**
 * 删除
 */
exports.destroy = async ctx => {
  ctx.validate({
    id: {
      type: 'mongo-id',
    },
  }, ctx.params);
  const data = await ctx.service.authApi.destroy();
  ctx.sendSuccess(data);
};

// eslint-disable-next-line jsdoc/require-param
/**
 * 更新
 */
exports.update = async ctx => {
  createValidate(ctx)
  const data = await ctx.service.authApi.update();
  ctx.sendSuccess(data);
};

